package object2D;

import byggeTegner.DrawPanel;
import context.*;

import geometry2D.MetricPoint2D;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.json.JSONException;
import org.json.JSONObject;

import util.FontStyle;
import util.StrokeStyle;
import util.Util2;

import model2D.Model2D;

// ***************************************************************************
// *** CLASS: TextAreaObject
// ***************************************************************************
public class TextAreaObject extends Object2D implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="object2D.TextAreaObject";
  static final String TYPE="TextAreaObject";
	
  private String[] TEXT;
  public String getText(int i) { return(TEXT[i]); }
  public void setText(int i,String newValue)
  { if(!TEXT[i].equals(newValue)) { String oldValue=TEXT[i]; TEXT[i]=newValue; FIRE_CHANGE_EVENT("TEXT["+oldValue+"==>"+newValue+"]"); } }

  // *************************************************************************
  // *** CONSTRUCTOR: TextAreaObject
  // *************************************************************************
  public TextAreaObject(Model2D objectModel,String id,MetricPoint2D p1,String[] lines) throws JSONException
  { super(objectModel,id);
    this.TEXT=lines;
    add(p1);
  }
  
  public TextAreaObject(Model2D objectModel,JSONObject jsonObject) throws JSONException
  { super(objectModel,jsonObject.getString("id"));
	if(DEBUG) Log.d(TAG,"<init>: jsonObject="+jsonObject);
    this.font=new FontStyle(jsonObject);
    this.stroke=new StrokeStyle(jsonObject);
    clearPointSet();
    add(new MetricPoint2D(jsonObject.getJSONArray("p1")));
//    TEXT[0]=jsonObject.optString("id1","");
//    TEXT[1]=jsonObject.optString("id2","");
//    TEXT[2]=jsonObject.optString("id3","");
  }


  public JSONObject toJSONObject() throws JSONException
  { JSONObject jsonObject=new JSONObject();
    jsonObject.put("type",TYPE);
    jsonObject.put("id",getIdent());
    MetricPoint2D p1=getMapLocation(0);
    jsonObject.put("p1",p1.encode());
//    jsonObject.put("id1",TEXT[0]);
//    jsonObject.put("id2",TEXT[1]);
//    jsonObject.put("id3",TEXT[2]);
    jsonObject.put("fill",fill.encode());
    jsonObject.put("stroke",stroke.encode());
	return(jsonObject);
  }

  // *************************************************************************
  // *** METHOD: paint
  // *************************************************************************
  public void paint(Graphics2D g,DrawPanel drawPanel)
  { MetricPoint2D p1=getMapLocation(0);
//    int x=(int)p1.getX();
//    int y=(int)p1.getY();
//    Font newFont=this.getFontStyle().getFont(drawPanel);
//    g.setFont(newFont);
//    int dy=8;
//    for(int i=0;i<TEXT.length;i++)
//    { g.drawString(TEXT[i],x,y); y=y+dy; }
    float x=(float)p1.getX();
    float y=(float)p1.getY();
    Font newFont=this.getFontStyle().getFont(drawPanel);
    float dy=newFont.getSize2D()*1.10f;
    for(int i=0;i<TEXT.length;i++)
    { Util2.drawString(g,TEXT[i],x,y,newFont,stroke,null,drawPanel.getPrintScale());
      y=y+dy;
    }
  }
  
  
  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    private JButton fillButton;
    
    public void paint(Graphics g)
    { if(DEBUG) Log.d(TAG,"EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
      fillButton.setBackground(fill.getColor());
  	super.paint(g);  
    }
  	
    public EditGeneralPanel(String title)
    { setBorder(Util2.getTitleBorder(title));
  	  setPreferredSize(new Dimension(350,120+60));
      setLayout(new GridLayout(6,2));
      add(new JLabel("       Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      for(int i=0;i<TEXT.length;i++)
      { add(new JLabel("       Field"+(i+1)+":"));
        add(textField(i));
      }
      fillButton=addEditFillButton(this,true);
      strokeButton=addEditStrokeButton(this,true);
    }
    private JTextField textField(final int i)
    { final JTextField textField1=new JTextField(getText(i));
      textField1.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setText(i,textField1.getText()); }
      });
      return(textField1);
    }
  }

}